package com.lz.helper.idcard

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Parcel
import android.os.Parcelable
import com.invs.invsIdCard
import com.invs.invswlt
import com.zkteco.android.IDReader.IDPhotoHelper
import com.zkteco.android.IDReader.WLTService
import java.util.*


class IDCardInfo(
    val name: String,
    val num: String,
    val sex: String,
    val nation: String, //民族
    val address: String,
    val birth: String,
    val photo: ByteArray,
    val finger: ByteArray,
    val readerType: Int //读取时的类型
) : Parcelable {

    internal constructor(card: invsIdCard) : this(
        card.name,
        card.idNo,
        card.sex,
        card.nation,
        card.address,
        card.birth,
        invswlt.Wlt2Bmp(card.wlt),
        if (card.finger == null) ByteArray(512) else Arrays.copyOfRange(card.finger, 0, 512),
        0
    )

    internal constructor(card: com.zkteco.android.biometric.module.idcard.meta.IDCardInfo) : this(
        card.name, card.id, card.sex, card.nation, card.address, card.birth, card.photo,
        if (card.fpdata == null) ByteArray(512) else Arrays.copyOfRange(card.fpdata, 0, 512),
        1
    )

    fun isMale(): Boolean = sex == "1"

    override fun toString(): String {
        return "IDCardInfo(name='$name', num='$num', sex='$sex', nation='$nation', address='$address', birth='$birth', photo = ${photo.size}, finger = ${finger.size})"
    }

    fun photoBitmap(): Bitmap? = if (readerType == 0) uPhotoBitmap() else serialPhotoBitmap()

    //u口时的身份证头像获取
    private fun uPhotoBitmap(): Bitmap? = try {
        BitmapFactory.decodeByteArray(photo, 0, 38862)
    } catch (_: Exception) {
        null
    }

    //串口形式时的身份证照片
    private fun serialPhotoBitmap(): Bitmap? = try {
        val buf = ByteArray(WLTService.imgLength)
        if (1 == WLTService.wlt2Bmp(photo, buf)) {
            IDPhotoHelper.Bgr2Bitmap(buf)
        } else {
            null
        }
    } catch (_: Exception) {
        null
    }

    companion object CREATOR : Parcelable.Creator<IDCardInfo> {
        override fun createFromParcel(parcel: Parcel): IDCardInfo {
            return IDCardInfo(parcel)
        }

        override fun newArray(size: Int): Array<IDCardInfo?> {
            return arrayOfNulls(size)
        }
    }

    override fun describeContents(): Int = 0

    constructor(parcel: Parcel) : this(
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.readString()!!,
        parcel.createByteArray()!!,
        parcel.createByteArray()!!,
        parcel.readInt()
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeString(num)
        parcel.writeString(sex)
        parcel.writeString(nation)
        parcel.writeString(address)
        parcel.writeString(birth)
        parcel.writeByteArray(photo)
        parcel.writeByteArray(finger)
        parcel.writeInt(readerType)
    }
}